import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.*;
/**
 * Write a description of class CarroObs here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CarroObs extends Obstaculos
{
    private GreenfootImage ima1;
    private Actor collided;
    private Actor collidedCarro;
    private Actor collidedTronco;
    private Actor collidedlad;
    private Ladron l;
    private static final int NUMP=40;
    
   public CarroObs(Ladron lad)
    {
        //ima1= new GreenfootImage("ladron.png");
        l=lad;
    }
    public void act() 
    {
        //super.act();
        mueveIzq();
        checaLimite();
    } 
    
    
    public void check()
    {
       
        World mundo = getWorld();
        /////////////
        int d=((Mundo)mundo).dameDano();
            int phO=l.getY()-((getImage().getHeight()/3)/2);
            int plO=l.getY()+((getImage().getHeight()/3)/2);
            int phF=getY()-(getImage().getHeight()/3)/2;
            int plF=getY()+(getImage().getHeight()/3)/2;
            int pihO=l.getY()+((getImage().getHeight()/2));
            int pilO=l.getY()-((getImage().getHeight()/2));
        
       int pxhO=l.getX()-((getImage().getWidth())/2);
        boolean destro=false;    
         
        
        if(plF < pilO)
        {
            mundo.setPaintOrder(Ladron.class, CarroObs.class);
        }
        else
            if(phF>pihO)
            {
                mundo.setPaintOrder(CarroObs.class, Ladron.class);
            }
            
        if( collided != null || getX() == 0)
        {
            destro = true;
        }
       
        /////////////************************************//////////*
        if(destro==true)
            if(getX() == 0)
            {
                getWorld().removeObject(this);
            }
            else
            explota();
    }
    
   public void mueveIzq()
    {
        World mundo=getWorld();
        if(((Mundo) getWorld()).getPause() == true)
            {
                setLocation(getX()-8, getY());
            }
    }
    
    public void checaLimite()
    {
        if(getX()==0)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void explota()
    {
         World mundo=getWorld();
         int d=((Mundo)mundo).dameDano();
         ((Mundo)mundo).cambiaDano(d-30);
         placePedazos(getX(), getY(),NUMP);
        getWorld().removeObject(this);
    }
     public void explota1()
    {
         World mundo=getWorld();
         placePedazos(getX(), getY(),NUMP);
        getWorld().removeObject(this);
    }
    private void placePedazos(int x, int y, int numPedazos)
    {
        for(int i=0; i<numPedazos;i++);
            getWorld().addObject(new PedCarro(),x,y);
    }
}

